package com.czk.sort.practice;

import java.util.Arrays;

/**
 * 冒泡排序的优化
 * @Author:ChenZhangKun
 * @Date: 2020/11/19 22:08
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] array={2,5,8,4,6,2,1,3,9,10,15,11,7};
        int[] array1={1,2,3,4,5};
        bubbleSort(array1);
        System.out.println(Arrays.toString(array1));
    }
    public static void bubbleSort(int[] array){
        boolean flag=false;
        int temp=0;
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j]>array[j+1]){
                    temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                    flag=true;
                }
            }
            if (!flag){
                // 没有交换，有序，直接退出
                System.out.println("没有交换直接退出");
                break;
            }else {
                flag=false;
            }
        }
    }
}
